//
//  AccountTool.m
//  微博2期
//
//  Created by J on 15/11/27.
//
//

#import "AccountTool.h"

//存储路径
#define PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"]
@implementation AccountTool



+ (void)saveAccount:(Account*)account{
    //自定义对象必须用归档
   [NSKeyedArchiver archiveRootObject:account toFile:PATH];

}



+(Account*)account{//如果帐号过期返回nil

    Account * account = [NSKeyedUnarchiver unarchiveObjectWithFile:PATH];
    
    //验证帐号是否过期
   long long  expires_in = [account.expires_in longLongValue];//过期s数
    
    NSDate * expiresTime= [account.create_time dateByAddingTimeInterval:expires_in];
    //获取当前时间
    NSDate * now = [NSDate date];
    //expiresTime < now 过期了    expiresTime > now 没过期
//    升序(右边大于左边)      相同 降序(左边大于右边)
//    {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending}
   NSComparisonResult result = [expiresTime compare:now];
    if (result != NSOrderedDescending) {//过期
        return nil ;
        
    }
//    NSLog(@"%@ %@",expiresTime,now);
    
    return [NSKeyedUnarchiver unarchiveObjectWithFile:PATH];

    
}
@end
